const db = wx.cloud.database();
const app = getApp();
const cm = db.collection("comments");
var tempComment=null;
var card=null;

Page({
    data: {
        comments: null,
        _id: null,
        type: null,
        orign: [],
        index: 0,
        inputValue: null,
        nickName: null,
        avatarUrl: null,
        focus: false
    },
    onLoad: function (e) {
        this.setData({
            _id: e.id,
            type: e.type
        })
        this.loadMsg();
        this.syncIfo();
        const that=this;
        db.collection('card').doc(parseInt(e.id)).get({
            success(e) {
                card=e.data
            }
        })
    },
    loadMsg() {
        const that = this;

        let cm_get = cm.where({
            key: that.data._id,
            type: 'card'
        }).orderBy("time", "desc");
        if (this.data.index != 0) {
            cm_get = cm_get.skip(this.data.index * 20);
        }

        cm_get.get({
            success(res) {
                res.data.forEach((r, i) => {
                    let date = new Date(r.time);
                    r.date = (date.getMonth() + 1) + "月" + date.getDate() + "日 " + date.getHours() + ":" + date.getMinutes();
                })
                that.setData({
                    comments: res.data
                })
            }
        })
    },
    comment(msg, cmt) {
        cmt = cmt ? cmt : {
            _id: card._id,
            nickName: card.name,
            avatarUrl: "cloud://cards-2c53b0.6361-cards-2c53b0-1258076431/xsmall/" + card._id + ".jpg",
            orign: []
        };

        if (!(this.data.avatarUrl && this.data.nickName)) {
            return;
        }
        if (!msg && msg!=0) {
            return;
        }
        const that = this;
        const time = new Date();
        let orign=cmt.orign;
        if (orign.length > 1) {
            orign[0] = {
                _id: cmt._id,
                nickName: cmt.nickName,
                avatarUrl: cmt.avatarUrl 
            }
        } else {
            orign.unshift({
                _id: cmt._id,
                nickName: cmt.nickName,
                avatarUrl: cmt.avatarUrl
            })
        }
        const comment_ = {
            key: that.data._id,
            type: 'card',
            orign: orign,
            comment: msg,
            nickName: that.data.nickName,
            avatarUrl: that.data.avatarUrl,
            time: time.getTime(),
            like: 0,
            reply: []
        };
        cm.add({
            data: comment_,
            success(e) {
                console.log("评论成功！",e);
                let comments=that.data.comments;
                let date=new Date();
                comment_.date = (date.getMonth() + 1) + "月" + date.getDate() + "日 " + date.getHours() + ":" + date.getMinutes();
                comments.unshift(comment_);
                that.setData({
                    comments,
                    inputValue: ''
                })
                tempComment=null;
                that.unfocus();
                const _ = db.command;
                cm.doc(cmt._id).update({
                    data: {
                        reply: _.push(e._id)
                    }
                });
            }
        })
    },
    commentMain() {
        let msg=this.data.inputValue;
        this.comment(msg,tempComment);
    },
    commentOthers(e) {
        tempComment=e.currentTarget.dataset.comments;
        this.setData({
            focus: true,
            current: tempComment.comment
        })
    },
    getUserInfo(e) {
        console.log(e)
        let userInfo = e.detail.userInfo;
        let nickName = userInfo.nickName;
        let avatarUrl = userInfo.avatarUrl;
        wx.setStorageSync("nickName", nickName);
        wx.setStorageSync("avatarUrl", avatarUrl);

        this.setData({
            nickName: nickName,
            avatarUrl: avatarUrl
        })
        this.comment(this.data.inputValue);
    },
    syncIfo() {
        let nickName=wx.getStorageSync("nickName");
        let avatarUrl=wx.getStorageSync("avatarUrl");
        let data = new Object();
        if (nickName) {
            data.nickName = nickName
        }
        if (avatarUrl) {
            data.avatarUrl = avatarUrl
        }
        this.setData(data)
    },
    say(e) {
        let msg=e.detail.value;
        this.setData({
            inputValue: msg
        })
    },
    unfocus() {
        this.setData({
            focus: false,
            current: null
        })
    }
})